package zelix.gui.clickguis.caesium.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import zelix.Core;
import zelix.gui.clickguis.caesium.components.GuiButton;
import zelix.gui.clickguis.caesium.components.GuiComboBox;
import zelix.gui.clickguis.caesium.components.GuiGetKey;
import zelix.gui.clickguis.caesium.components.GuiLabel;
import zelix.gui.clickguis.caesium.components.GuiSlider;
import zelix.gui.clickguis.caesium.components.GuiToggleButton;
import zelix.gui.clickguis.caesium.components.listeners.ComboListener;
import zelix.gui.clickguis.caesium.components.listeners.ComponentListener;
import zelix.gui.clickguis.caesium.components.listeners.KeyListener;
import zelix.gui.clickguis.caesium.components.listeners.ValueListener;
import zelix.hack.Hack;
import zelix.value.BooleanValue;
import zelix.value.ModeValue;
import zelix.value.NumberValue;
import zelix.value.Value;

/**
 * @author sendQueue <Vinii>
 *
 *         Further info at Vinii.de or github@vinii.de, file created at
 *         11.11.2020. Use is only authorized if given credit!
 * 
 */
public class ComponentsListener extends ComponentListener {
	private GuiButton button;

	public ComponentsListener(GuiButton button) {
		this.button = button;
	}

	@Override
	public void addComponents() {
		add(new GuiLabel("Settings >"));
		final Hack m = Core.hackManager.getHack(button.getText());

		for (Value set : m.getValues()) {
			//////System.out.println(m.getName());
			//////System.out.println(set.getName());
			if (set instanceof BooleanValue){
				GuiToggleButton toggleButton = new GuiToggleButton((BooleanValue)set);
				toggleButton.setToggled(((BooleanValue) set).getValue());
				toggleButton.addClickListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						//set.setValue();
						set.setValue(toggleButton.isToggled());
						new Thread(() -> {
							//Core.hackManager.saveDetails();
						}).start();
					}
				});
				add(toggleButton);
				//break;
			}
			if (set instanceof NumberValue){
				GuiSlider slider = new GuiSlider((NumberValue)set, ((NumberValue) set).getMin(), ((NumberValue) set).getMax(), ((NumberValue)set).getValue(),2);
				slider.addValueListener(new ValueListener() {

					@Override
					public void valueUpdated(double value) {
						set.setValue(value);
					}

					@Override
					public void valueChanged(double value) {
						set.setValue(value);
						new Thread(() -> {
							//Core.hackManager.saveDetails();
						}).start();
					}
				});
				add(slider);
				//break;
			}
			if (set instanceof ModeValue){
				GuiComboBox comboBox = new GuiComboBox((ModeValue)set);
				comboBox.addComboListener(new ComboListener() {

					@Override
					public void comboChanged(String combo) {
						new Thread(() -> {
							//Core.hackManager.saveDetails();
						}).start();
					}
				});
				add(comboBox);
				//break;
			}
		}
		GuiGetKey ggk = new GuiGetKey("KeyBind", m.getKey());
		ggk.addKeyListener(new KeyListener() {

			@Override
			public void keyChanged(int key) {
				m.setKey(key);
				new Thread(() -> {
					//Core.hackManager.();
				}).start();

			}
		});
		add(ggk);
	}

}
